<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/27 0027
 * Time: 下午 2:26
 */

namespace console\controllers;

use common\components\DataHelper;
use common\models\Admin\Admin;

class InitController extends \yii\console\Controller
{
    public $username;

    public $password;

    public function options($actionID)
    {
        return ['username', 'password'];
    }

    public function optionAliases()
    {
        return ['u'=>'username', 'p'=>'password'];
    }

    /**
     * php yii init -u=admin -p=123456
     */
    public function actionIndex()
    {
        $admin = new Admin();
        $adminRet = $admin->getListAll(['id'=>Admin::getRootIds()]);

        if ( $adminRet ) {
            $this->stderr('Root account already exists! Could not initialize!');
            die(1);
        }

        $admin->id = Admin::DEFAULT_ROOT_ID;
        $admin->username = $this->username;
        $admin->password = $this->password;
        $admin->status = Admin::STATUS_YES;
        $admin->created_time = DataHelper::date();
        $admin->type = 0;
        if ( $admin->insert() ) {
            $this->stdout("SUCCESS!");
        } else {
            $this->stderr($admin->getFirstErrorToStr());
        }
    }
}